/**####################################################################################
  CLASS : mydojo.dijix.ReportGrid
  ==================================
  - A simple grid widget with form elements from a remote location
  - Same level as SimpleGrid/DbGrid/TitledGrid
  
  ####################################################################################*/
dojo.provide("mydojo.dijix.ReportGrid");

dojo.require("mydojo.dijix.SimpleGrid");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.BorderContainer");

dojo.declare("mydojo.dijix.ReportGrid", dijit.layout.ContentPane , {
				
		headerUrl		: "",	
		// Widget Refs....
		bcWt			: "",
		headerWt		: "",
		gridWt			: "",
		footerWt		: "",		
		trWt			: "",		
		// ======================================================================
		//	Post create widget
		// ----------------------------------------------------------------------
		postCreate: function() {				
			this.inherited(arguments);	
			// create BC
			var rootNode = document.createElement("div");
			this.bcWt = new dijit.layout.BorderContainer( {id:this.id+"_bc" , style:"width:100%;height:100%"} , rootNode );
			this.domNode.appendChild(this.bcWt.domNode);
			
			// create TOP CP -- Report Form						
			this.headerWt = new dijit.layout.ContentPane( {id:this.id+"_top" , style:"height:auto;overflow:hidden" , region:"top" , href:this.headerUrl});
			this.bcWt.addChild(this.headerWt);
			var doo = function(_bc,_header){ _header.onLoad = function(){ _bc.layout() } };
			doo(this.bcWt,this.headerWt);
			
			// create CENTER CP	-- this is actually SimpleGrid					
			this.gridWt = new mydojo.dijix.SimpleGrid( {id:this.id+"_cen" , region:"center" , data:this.data});
			this.bcWt.addChild(this.gridWt);
			
			
			// create BOTTOM CP - Summary
			this.footerWt = new dijit.layout.ContentPane( {id:this.id+"_btt" , style:"background:#BDD5E4;height:20px;border-bottom:1px solid #000" , region:"bottom"});
			this.bcWt.addChild(this.footerWt);
								
			// create footer
			this.trWt = document.createElement("span");
			this.trWt.id = this.id+"_totRec";
			this.footerWt.setContent("<span class='text2'>Total: </span>");
			this.footerWt.domNode.appendChild(this.trWt);
			
			this.bcWt.startup();
			this.bcWt.layout();	
			
			this.setTotalRecs(0);												
		},
		// ======================================================================
		//	Render response from server 
		// ----------------------------------------------------------------------	
		render: function(response,event){
			this.renderGrid(response.model);
		},
		// ======================================================================
		//	Clean up this widget
		// ----------------------------------------------------------------------	
		clean: function(){
			console.log("Running DbGrid.clean() function.")
			this.headerWt.destroyDescendants();
			this.headerWt.destroy();
			this.gridWt.clean();
			this.gridWt.destroyDescendants();
			this.gridWt.destroy();
			this.footerWt.destroyDescendants();
			this.footerWt.destroy();
			this.bcWt.destroyDescendants();
			this.bcWt.destroy();
		},		
		// ======================================================================
		//	Re Structure grid
		// ----------------------------------------------------------------------
		reStruct: function(struct){			
			this.gridWt.reStruct(struct);
		},	
		// ======================================================================
		//	Render grid with given data
		// ----------------------------------------------------------------------
		renderGrid: function(data){
			if(data)
			{
				this.gridWt.renderGrid(data);
				this.setTotalRecs( this.gridWt.grid.model.count);
			}		
		},	
		// ======================================================================
		//	Clean the grid data
		// ----------------------------------------------------------------------
		clearGrid: function(){
			this.gridWt.clearGrid();
			this.setTotalRecs( this.gridWt.grid.model.count);
		},	
		// ======================================================================
		//	Set Total Records
		// ----------------------------------------------------------------------
		setTotalRecs: function(val){
			this.trWt.innerHTML = val;			
		}
});
